//
//  PCOrderManager.m
//  PromotionCode
//
//  Created by baken on 2018/2/11.
//  Copyright © 2018年 My. All rights reserved.
//

#import "PCOrderManager.h"
#import "PCMyOrderModel.h"
#import "PCGoodsModel.h"
#import "PCInpourModel.h"
#import "PCWXInpourModel.h"
#import "PCLocalPayModel.h"
#import "U9EncryptManager.h"
#import "PCOrderDetailModel.h"
@implementation PCOrderManager

+ (void)getMyOrderListWithUserID:(NSString *)uid andSessionID:(NSString *)sessionid andCompletionHandle:(void (^)(PCMyOrderModel *, NSError *))complete{
    [self EncryptPOST:[NSString stringWithFormat:@"%@%@",Api_URL,kMyOrder]
           parameters:[NSString stringWithFormat:@"userid=%@&sessionid=%@",uid,sessionid]
    completionHandler:^(id responseObj, NSError *error) {
        PCMyOrderModel *model = [PCMyOrderModel mj_objectWithKeyValues:responseObj];
        if (model.error_code != 0) {
            error = [self createError:model.error_msg];
        }
        complete(model,error);
    }];
}

+(void)getOrderDetailWithUserID:(NSString *)uid andSessionID:(NSString *)sessionid andOrder_sn:(NSString *)order_sn andCompletionHandle:(void (^)(PCOrderDetailModel *model, NSError *error))complete{
    [self EncryptPOST:[NSString stringWithFormat:@"%@%@",Api_URL,kOrderDetail]
           parameters:[NSString stringWithFormat:@"userid=%@&sessionid=%@&order_sn=%@",uid,sessionid,order_sn]
    completionHandler:^(id responseObj, NSError *error) {
        PCOrderDetailModel *model = [PCOrderDetailModel mj_objectWithKeyValues:responseObj];
        if (model.error_code != 0) {
            error = [self createError:model.error_msg];
        }
        complete(model,error);
    }];
}

+ (void)cancelMyOrderListWithUserID:(NSString *)uid andSessionID:(NSString *)sessionid andOrderID:(NSString *)orderID andCompletionHandle:(void (^)(PCMyOrderModel *, NSError *))complete{
    [self EncryptPOST:[NSString stringWithFormat:@"%@%@",Api_URL,kOrderCancel]
           parameters:[NSString stringWithFormat:@"userid=%@&sessionid=%@&ordersn=%@",uid,sessionid,orderID]
    completionHandler:^(id responseObj, NSError *error) {
        PCMyOrderModel *model = [PCMyOrderModel mj_objectWithKeyValues:responseObj];
        if (model.error_code != 0) {
            error = [self createError:model.error_msg];
        }
        complete(model,error);
    }];
}


+(void)freeBuyALiPayWithMoney:(NSDecimalNumber *)money andPid:(NSString *)pid andMonth:(NSString *)month andAttr:(NSString *)attr UserID:(NSString *)uid andSessionID:(NSString *)sessionid andCompletionHandle:(void (^)(InpourModel *, NSError *))complete{
    [self EncryptPOST:[NSString stringWithFormat:@"%@%@",Api_URL,kOrderPayALi]
           parameters:[NSString stringWithFormat:@"userid=%@&money=%@&sessionid=%@&pid=%@&month=%@&attr=%@",uid,money,sessionid,pid,month,attr]
    completionHandler:^(id responseObj, NSError *error) {
        PCInpourModel *model = [PCInpourModel mj_objectWithKeyValues:responseObj];
        if (model.error_code != 0) {
            error = [self createError:model.error_msg];
        }
        complete(model.output,error);
    }];
    
}

+(void)freeBuyWXWithMoney:(NSDecimalNumber *)money andPid:(NSString *)pid UserID:(NSString *)uid andSessionID:(NSString *)sessionid andCompletionHandle:(void (^)(WXInpourModel *, NSError *))complete{
    [self EncryptPOST:[NSString stringWithFormat:@"%@%@",Api_URL,kOrderPayWX]
           parameters:[NSString stringWithFormat:@"userid=%@&money=%@&sessionid=%@&pid=%@",uid,money,sessionid,pid]
    completionHandler:^(id responseObj, NSError *error) {
        PCWXInpourModel *model = [PCWXInpourModel mj_objectWithKeyValues:responseObj];
        if (model.error_code != 0) {
            error = [self createError:model.error_msg];
        }
        complete(model.output,error);
    }];
    
}
/*
 1. 获取支付参数
 接口：/product2/get_gogo_pay
 参数：userid，money 支付金额， pid 商品id，month 分期信息
 返回：orderString（返回字段：subject交易说明，order_sn唯一订单号，total_amount支付金额，instalment分期期数，token支付密钥）
 */
+(void)getLocalPayInfoWithMoney:(NSDecimalNumber *)money andPid:(NSString *)pid andMonth:(NSString *)month andAttr:(NSString *)attr UserID:(NSString *)uid andSessionID:(NSString *)sessionid andOrder_sn:(NSString *)order_sn andCompletionHandle:(void (^)(PCLocalPayModel *, NSError *))complete{
    [self EncryptPOST:[NSString stringWithFormat:@"%@%@",Api_URL,kGetLocalPayInfo]
           parameters:[NSString stringWithFormat:@"userid=%@&money=%@&sessionid=%@&pid=%@&month=%@&attr=%@&order_sn=%@"
                       ,uid,money,sessionid,pid,month,attr,order_sn]
    completionHandler:^(id responseObj, NSError *error) {
        PCLocalPayModel *model = [PCLocalPayModel mj_objectWithKeyValues:responseObj];
        if (model.error_code != 0) {
            error = [self createError:model.error_msg];
        }
        complete(model,error);
    }];
    
    
}

/*
 2.发起支付
 接口：/product2/gogopay
 参数：userid，order_sn唯一订单号，key token加密串
 说明 参数key加密方式md5('用户id+支付金额+分期期数+唯一订单号+支付token')
 gogo支付token=f57120632cf6e5089ff2feea744dc018
 */
+(void)freeBuyLocalWithMoney:(NSDecimalNumber *)total_amount andOrder_sn:(NSString *)order_sn andAttr:(NSString *)attr andToken:(NSString *)token andUserID:(NSString *)uid andSession:(NSString *)session andInstalment:(NSString *)instalment andCompletionHandle:(void (^)(WXInpourModel *, NSError *))complete{
    NSString *paraStr = [NSString stringWithFormat:@"%@%@%@%@%@",uid,total_amount,instalment,order_sn,token];
    NSString *md5Str = [[U9EncryptManager md5:paraStr] lowercaseString];
    [self EncryptPOST:[NSString stringWithFormat:@"%@%@",Api_URL,kOrderPayLocal]
           parameters:[NSString stringWithFormat:@"userid=%@&order_sn=%@&key=%@&sessionid=%@",uid,order_sn,md5Str,session]
    completionHandler:^(id responseObj, NSError *error) {
        PCWXInpourModel *model = [PCWXInpourModel mj_objectWithKeyValues:responseObj];
        if (model.error_code != 0) {
            error = [self createError:model.error_msg];
        }
        complete(model.output,error);
    }];
    
}


@end
